package com.estronger.shopmanage.utils;

import android.content.Context;
import android.text.TextUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MD5Utils {

	/**
	 * md5加密
	 * 
	 * @param password
	 * @return
	 */
	public static String md5Password(String password) {

		try {
			// 获取java自带的MD5加密类
			MessageDigest digest = MessageDigest.getInstance("md5");
			byte[] result = digest.digest(password.getBytes());
			StringBuffer buffer = new StringBuffer();
			// 加密加盐0xff;
			for (byte b : result) {
				int number = b & 0xff;
				String str = Integer.toHexString(number);
				// System.out.println(str);
				if (str.length() == 1) {
					buffer.append("0");
				}
				buffer.append(str);
			}

			// 返回加密的字符串
			return buffer.toString();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}

	}
	// 验证手机号码格式是否正确
	public static boolean isRightPhoneNumber(Context context, String number) {
		if (!TextUtils.isEmpty(number)) {
			Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
			Matcher m = p.matcher(number);
//			if(m.matches()||(number.contains("-")&&number.length()==12)){
//				return true;
//			}else{
//				Toast.show(this,"电话号码格式错误:" + number,2000);
//				return false;
//			}
			return true;
		} else {
			Toast.show(context, "电话号码不能为空", 2000);
			return false;
		}
	}
}
